
package com.epam.android.training.app.activities;

import com.epam.android.training.app.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;

/**
 * Activity with splash screen. For now just shows some picture
 * 
 * @author Natallia_Kukharchyk
 */
public class SplashScreenActivity extends Activity {
  protected boolean active = true;

  private static String TAG = "SplashScreenActivity";

  protected int splashTime = 3500; // time to display the splash screen

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    // thread for displaying the SplashScreen
    Thread splashTread = new Thread() {
      @Override
      public void run() {
        try {
          int waited = 0;
          while (active && (waited < splashTime)) {
            sleep(100);
            if (active) {
              waited += 100;
            }
          }
        } catch (InterruptedException e) {
          Log.i(TAG, e.toString());
        } finally {
          finish();
          Intent intent = new Intent(getApplicationContext(), MainActivity.class);
          startActivity(intent);
          stop();
        }
      }
    };
    splashTread.start();
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    // close splash screen on touch
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
      active = false;
    }
    return true;
  }
}
